<?php
namespace Home\Logic;

import("Org.ThinkSDK.ThinkOauth");

class ThirdPartyLoginLogic {
	//登录成功，获取腾讯QQ用户信息
	public function qq($token){
		$qq   = \ThinkOauth::getInstance('qq', $token);
		$data = $qq->call('user/get_user_info');

		if($data['ret'] == 0){
			$userInfo['type'] = 'QQ';
			$userInfo['name'] = $data['nickname'];
			$userInfo['nick'] = $data['nickname'];
			$userInfo['head'] = $data['figureurl_2'];
			return $userInfo;
		} else {
			throw_exception("获取腾讯QQ用户信息失败：{$data['msg']}");
		}
	}

	//登录成功，获取新浪微博用户信息
	public function sina($token){
		$sina = \ThinkOauth::getInstance('sina', $token);
		$data = $sina->call('users/show', "uid={$sina->openid()}");

		if($data['error_code'] == 0){
			$userInfo['type'] = 'SINA';
			$userInfo['name'] = $data['name'];
			$userInfo['nick'] = $data['screen_name'];
			$userInfo['head'] = $data['avatar_large'];
			return $userInfo;
		} else {
			throw_exception("获取新浪微博用户信息失败：{$data['error']}");
		}
	}
	
	//支付宝登录
	public static function alipay($token){
		$alipay = \ThinkOauth::getInstance('alipay', $token);
		$param=array(
				'_input_charset'=>'utf-8',
				'input_charset'=>'utf-8',
				'service'=>'mobile.common.login.userInfo.query',
				'token'=>$token['access_token'],
				'format'=>'json',
		);
		$data=$alipay->call('user/register',$param);
		return $data;
	}
	
    //登录成功，微信用户信息
	public function weixin($token){
		$weixin   = \ThinkOauth::getInstance('weixin', $token);
		$data = $weixin->call('sns/userinfo');

		if($data['ret'] == 0){
			$userInfo['type'] = 'WEIXIN';
			$userInfo['name'] = $data['nickname'];
			$userInfo['nick'] = $data['nickname'];
			$userInfo['head'] = $data['headimgurl'];
			return $userInfo;
		} else {
			throw_exception("获取微信用户信息失败：{$data['errmsg']}");
		}
	}
}